package com.infore.airsupervise.ui.fragment;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.github.gcacace.signaturepad.views.SignaturePad;
import com.infore.airsupervise.R;
import com.infore.airsupervise.other.listener.SignatrueViewListener;
import com.infore.airsupervise.ui.widget.SignatrueLayout;
import com.infore.airsupervise.utils.ImageUtil;
import com.infore.airsupervise.utils.ToastUtil;
import com.rey.material.app.BottomSheetDialog;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 签名板 framgent
 * Created by xiaotao on 2017/5/16.
 */

public class SingnatrueFragment extends BaseFragment implements SignatrueViewListener {

    @BindView(R.id.show_photo_recycler_view)
    RecyclerView recyclerView;

    private BottomSheetDialog bottomSheetDialog;
    private SignatrueLayout signatrueLayout ;

    private List<String> signatrueList ;
    private BaseQuickAdapter<String,BaseViewHolder> adapter;
    private LayoutInflater inflater;


    public SingnatrueFragment() {
    }

    @SuppressLint("ValidFragment")
    public SingnatrueFragment(List<String> signatrueList) {
        this.signatrueList = signatrueList == null ? new ArrayList<String>(): signatrueList;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        this.inflater = inflater;
        View view = inflater.inflate(R.layout.fragment_show_photo, container, false);
        unbinder = ButterKnife.bind(this, view);
        //设置布局管理器
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext());
        linearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
        recyclerView.setLayoutManager(linearLayoutManager);
        //设置适配器
        initAdapter();
        return view;
    }

    /**
     * 适配器设置
     */
    private void initAdapter(){
        adapter = new BaseQuickAdapter<String, BaseViewHolder>(R.layout.item_select_photo,signatrueList) {
            @Override
            protected void convert(final BaseViewHolder helper, String item) {
                item = "file://"+item;
                ImageView delete = helper.getView(R.id.delete_btn);
                delete.setVisibility(View.VISIBLE);
                delete.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        adapter.remove(helper.getAdapterPosition());
                    }
                });
//                ViewGroup.LayoutParams lp = helper.getConvertView().getLayoutParams();
//                lp.width = 100;
//                lp.height = 100;
//                helper.getConvertView().setLayoutParams(lp);
                ImageUtil.getInstance().displayImage((ImageView) helper.getView(R.id.image_view), item,100 ,100 );
            }
        };
        adapter.addFooterView(getFootView());
        recyclerView.setAdapter(adapter);
    }

    private  View getFootView(){
        View view = inflater.inflate(R.layout.item_select_photo,null);
        ImageView img = (ImageView) view.findViewById(R.id.image_view);
        img.setImageResource(R.drawable.ic_add_image);
        img.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //添加签名
                showBottomLayout();
            }
        });
        return view ;
    }


    private void showBottomLayout(){
       signatrueLayout = new SignatrueLayout(getContext());
        signatrueLayout.setSignatrueViewListener(this);
        bottomSheetDialog = new BottomSheetDialog(getContext(), R.style.Material_App_BottomSheetDialog);
        bottomSheetDialog.heightParam(ViewGroup.LayoutParams.WRAP_CONTENT)
                .contentView(signatrueLayout.getContentView());
        bottomSheetDialog.show();
    }

    public List<String> getSignatrueList(){
        return this.signatrueList;
    }

    @Override
    public void clearListener() {

    }

    @Override
    public void saveSuccess(String filePath) {
        //ToastUtil.showShort(getContext(),"保存成功\n"+filePath);
        adapter.addData(filePath);
        bottomSheetDialog.dismiss();
    }

    @Override
    public void saveFailed() {
        ToastUtil.showLong(getContext(),"保存失败");
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
    }
}
